﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoUpdateMgr
{
    using AutoUpdateMgr.Mgr;
    using System.IO;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private AutoUpdate.Model.Model.fileTransfer.fs_version versionModel { set; get; } = null;

        private List<AutoUpdate.Model.Model.fileTransfer.fs_version> versionList { set; get; } = null;

        private List<AutoUpdate.Model.Model.fileTransfer.fs_inode> nodeList { set; get; } = null;

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //选择文件夹
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    string curDir = fbd.SelectedPath;

                    Console.WriteLine(curDir);

                    Queue<string> dirQueue = new Queue<string>();

                    List<string> fileList = new List<string>();

                    nodeList = new List<AutoUpdate.Model.Model.fileTransfer.fs_inode>();

                    dirQueue.Enqueue(curDir);

                    while (dirQueue.Count > 0)
                    {
                        string dirPath = dirQueue.Dequeue();

                        foreach (string line in AutoUpdate.Utility.Helper.FileHelper.getSubDirectories(dirPath))
                        {
                            Console.WriteLine(line);

                            dirQueue.Enqueue(line);
                        }

                        foreach (string line in AutoUpdate.Utility.Helper.FileHelper.getSubFiles(dirPath))
                        {
                            Console.WriteLine(line);

                            if (File.Exists(line))
                            {
                                FileInfo info = new FileInfo(line);

                                if (info != null)
                                {
                                    AutoUpdate.Model.Model.fileTransfer.fs_inode node = new AutoUpdate.Model.Model.fileTransfer.fs_inode();

                                    node.name = info.Name;

                                    node.realPath = info.FullName;

                                    node.length = info.Length;

                                    node.relativePath = node.realPath.Replace(curDir, "");

                                    node.md5 = AutoUpdate.Utility.Helper.FileHelper.fileMd5(line);

                                    node.extendName = info.Extension;

                                    nodeList.Add(node);

                                }
                            }
                        }
                    }

                    this.dataGridView1.DataSource = nodeList;

                }
            }
        }

        public static Form1 frmMain { set; get; } = null;

        private void Form1_Load(object sender, EventArgs e)
        {
            frmMain = this;

            this.dataGridView1.AutoGenerateColumns = false;

            Mgr.ConfigMgr.getInstance().Load();

            Mgr.ClientMgr.getInstance().ConnectToServer();





        }

        public void changeNetworkStatus(AutoUpdate.Model.Enum.NetworkStatusEnum networkStatus)
        {
            if (this != null)
            {
                this.Invoke(new Action(() =>
                {
                    switch (networkStatus)
                    {
                        case AutoUpdate.Model.Enum.NetworkStatusEnum.Connected:
                            this.toolStripStatusLabel2.Text = "网络状态：已连接";
                            break;
                        case AutoUpdate.Model.Enum.NetworkStatusEnum.Connecting:
                            this.toolStripStatusLabel2.Text = "网络状态：连线中";
                            break;
                        case AutoUpdate.Model.Enum.NetworkStatusEnum.DisConnected:
                            this.toolStripStatusLabel2.Text = "网络状态：已断开";
                            break;
                        case AutoUpdate.Model.Enum.NetworkStatusEnum.ReConnecting:
                            this.toolStripStatusLabel2.Text = "网络状态：断线重连中";
                            break;
                    }

                }));
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FormCreateVersion frmCreateVersion = new FormCreateVersion();

            frmCreateVersion.callback = new Action<AutoUpdate.Model.Packet.Packet>((AutoUpdate.Model.Packet.Packet packet) =>
            {
                Console.WriteLine(packet.MessageContent);

                this.versionList = AutoUpdate.Utility.Helper.JsonHelper.getObject<List<AutoUpdate.Model.Model.fileTransfer.fs_version>>(packet.MessageContent);

                this.listBox1.DisplayMember = "version";

                this.listBox1.ValueMember = "version";

                this.listBox1.DataSource = this.versionList;

            });

            frmCreateVersion.ShowDialog();


        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            //上传文件
            try
            {
                FormFileUpload frmFileUpload = new FormFileUpload();

                this.versionModel.files = nodeList;

                frmFileUpload.currentVersion = this.versionModel;

                frmFileUpload.ShowDialog();

                this.getVersionList();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex > -1)
            {
                string verion = this.listBox1.SelectedValue.ToString();


                this.versionModel = this.versionList.FirstOrDefault(a => a.version == verion);

                this.toolStripStatusLabel1.Text = "当前版本：" + this.versionModel.version;

                Action<AutoUpdate.Model.Packet.Packet> action = new Action<AutoUpdate.Model.Packet.Packet>((AutoUpdate.Model.Packet.Packet packet) =>
                {
                    Console.WriteLine(packet.MessageContent);

                    this.Invoke(new Action(() =>
                   {
                       this.dataGridView1.DataSource = null;

                       this.versionModel = AutoUpdate.Utility.Helper.JsonHelper.getObject<AutoUpdate.Model.Model.fileTransfer.fs_version>(packet.MessageContent);

                       this.dataGridView1.DataSource = this.versionModel.files;
                   }));



                });

                AutoUpdate.Model.Packet.RequestPacket request = new AutoUpdate.Model.Packet.RequestPacket();

                request.CommandText = "getVersion";

                request.MessageContent = this.versionModel.version;

                ActionMgr.getInstance().addAction(request.PacketID, action);

                ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request));
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            this.getVersionList();
        }

        private void getVersionList()
        {
            AutoUpdate.Model.Packet.RequestPacket request = new AutoUpdate.Model.Packet.RequestPacket();

            request.CommandText = "getVersions";

            Action<AutoUpdate.Model.Packet.Packet> action = new Action<AutoUpdate.Model.Packet.Packet>((AutoUpdate.Model.Packet.Packet packet) =>
            {
                Console.WriteLine(packet.MessageContent);

                this.versionList = AutoUpdate.Utility.Helper.JsonHelper.getObject<List<AutoUpdate.Model.Model.fileTransfer.fs_version>>(packet.MessageContent);

                this.listBox1.DisplayMember = "version";

                this.listBox1.ValueMember = "version";

                this.listBox1.DataSource = this.versionList;

            });

            ActionMgr.getInstance().addAction(request.PacketID, action);

            ClientMgr.getInstance().client.sendMessage(AutoUpdate.Utility.Helper.JsonHelper.getString(request));
        }
    }
}
